#include "threadFunc.h"
#include "tcpServerInit.h"
#include "puts_gets.h"
#include "getLocalAddr.h"
#include "transMessage.h"
void *threadFunc(void *arg)
{
    
    threadInfo_t *pthreadInfo = (threadInfo_t *)arg;
    int filesockfd;
    
    pthread_mutex_lock(&pthreadInfo->mutex);
    pthreadInfo->portAddNUM++;
    uint16_t port = (uint16_t)((pthreadInfo->portAddNUM)%100+20000);
    pthread_mutex_unlock(&pthreadInfo->mutex);

    tcpServerInit(pthreadInfo->longcmd.userinfo.netinfo.ip, port, &filesockfd);
    getLocalAddr(filesockfd, &pthreadInfo->longcmd);

    transCommand(pthreadInfo->sockfd, &pthreadInfo->longcmd);

    int netfd = accept(filesockfd, NULL, NULL);
    if(pthreadInfo->longcmd.cmdType == CMD_PUTS){
        printf("begin puts files.\n");
        int ret = putsfiles(&pthreadInfo->longcmd, netfd);
        if (ret != 0)
        {
            printf("putsfiles failed!\n");
        }
    }else if(pthreadInfo->longcmd.cmdType == CMD_GETS){
        printf("begin gets files.\n");
        int ret = getsfiles(&pthreadInfo->longcmd, netfd);
        if (ret!= 0)
        {
            printf("getsfiles failed!\n");
        }
    }
    
    
    close(netfd);
    close(filesockfd);
    return NULL;
}
